module.exports = function ($scope, $state, $resource, $modal, toaster) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
	};
	$scope.pageInfo = {
		
	};
	$scope.pageInfo.pageNo = 1;		//当前页码
	$scope.pageInfo.pageSize = 10;	//每页显示几条
	$scope.pageInfo.totalItems = 0;	//总条数
	//卡列表
	$scope.loadlist = function () {
		
		$resource('/api/as/mc/shopmenu/myMenuPageList', {}, {}).save($scope.pageInfo, function (res) {
			if (res.errcode === 0) {
				$scope.beanList = res.data.results;
				$scope.pageInfo.totalItems = res.data.totalRecord;
				angular.forEach($scope.beanList, function(item){
					if(item.menu_state == 0){
						item.selected = true;
					}else{
						item.selected = false;
					}
        });
        angular.forEach($scope.beanList, function(items){
					if(items.img && items.img!=''){
						items.showflg = true;
					}else{
						items.showflg = false;
					}
        });
        console.log($scope.beanList);
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

	$scope.loadlist();

	//房间信息
	$scope.info = function (bean) {
		var modalInstance = $modal.open({
			template: require('../views/skwxShopMyMenuInfo.html'),
			controller: 'skwxShopMyMenuInfo',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
	}

	//删除卡
	$scope.delete = function (bean) {
		if (confirm('确认删除此条记录吗？') == true) {
			$resource('/api/as/mc/shopmenu/deleteMyMenu', {}, {}).save(bean, function (res) {
				if (res.errcode === 0) {
					$scope.loadlist();
				} else {
					toaster.error({ title: '', body: res.errmsg });
				}
			});
		}
	}


	$scope.change = function(info) {
		console.log(info)
		var para = {
			'menu_state': info.selected ? '0' : '1',
			'id': info.id
		}
		$resource('/api/as/mc/shopmenu/updateMyMenu', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '设置成功。'});
			}else{
				toaster.error({title: '', body: '设置失败请重试。'});
			}
		});
	}


	$scope.createNowMyMenu = function() {
		
		$resource('/api/as/mc/shopmenu/createNowMyMenu', {}, {}).save({}, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '同步成功。'});
				$scope.loadlist();
			}else{
				toaster.error({title: '', body: res.errmsg});
			}
		});
	}
};